package com.sny.util;

import com.sny.constants.FileConstants;
import com.sny.vo.param.FileUploadRequestParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
@Slf4j
public class FilePathUtil implements ApplicationRunner {

    @Value("${upload.root.dir}")
    private String uploadRootDir;

    @Value("${upload.window.root}")
    private String uploadWindowRoot;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        createUploadRootDir();
    }


    private void createUploadRootDir() {
        String path = getBasePath();
        File file = new File(path);
        if (!file.mkdirs()) {
            file.mkdirs();
        }
    }


    public String getPath() {
        return uploadRootDir;
    }

    public String getBasePath() {
        String path = uploadRootDir;
        if (SystemUtil.isWinOs()) {
            path = uploadWindowRoot + uploadRootDir;
        }

        return path;
    }

    /**
     * 获取大文件上传基础路径
     *
     * @param param
     * @return java.lang.String
     * @author wangKesheng
     * @date 2022/4/17 18:32
     */
    public String getPath(FileUploadRequestParam param) {
        String path = this.getBasePath() + FileConstants.FILE_SEPARATORCHAR + param.getAppId() + FileConstants.FILE_SEPARATORCHAR + param.getMd5();
        return path;
    }

  /**
   * 获取大文件上传conf路径
   *
   * @param param
   * @return java.lang.String
   * @author wangKesheng
   * @date 2022/4/17 18:32
   */
  public String getConfPath(FileUploadRequestParam param) {
    String path = getPath(param) + FileConstants.FILE_SEPARATORCHAR + param.getMd5() + ".conf";
    return path;
  }

  /**
   * 获取大文件上传conf路径
   *
   * @param param
   * @return java.lang.String
   * @author wangKesheng
   * @date 2022/4/17 18:32
   */
  public String getTmpFilePath(FileUploadRequestParam param) {
    String path = getPath(param) + FileConstants.FILE_SEPARATORCHAR + param.getMd5() + "_tmp";
    return path;
  }

    public String getPathFileId(String fileId) {
        String path = this.getBasePath() + FileConstants.FILE_SEPARATORCHAR + fileId;
        return path;
    }

}
